package extract;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IMDb {

	public FileWriter fw;
	public BufferedWriter bw;
	
	public void read()
	{
		try {
			FileReader reader=new FileReader("raw_source.txt");
			BufferedReader br=new BufferedReader(reader);
			String content="";
			while(br.ready()){
				content+=br.readLine();
			}
			br.close();
			reader.close();

			fw=new FileWriter("names.txt");
			bw=new BufferedWriter(fw);
			
			
			setNames(content);
			
			bw.close();
			fw.close();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void setNames(String content) throws Exception{
		Pattern p=Pattern.compile(RegEx.IMDb, Pattern.DOTALL);
		Matcher m=p.matcher(content);

		while(m.find())
		{
			//System.out.println(m.group(1));
			String s=m.group(1);
			s=s.replace("&#x27;", "").replace(":", "").replace(",", "").replace(".", "");
			s=s.replace(" ", "-");
			s=s.toLowerCase();
						
			bw.write(s+";\n");
			
			URL movie = new URL("http://www.jinni.com/movies/"+s);
			URLConnection yc = movie.openConnection();
			
			try{
			BufferedReader in = new BufferedReader(
					new InputStreamReader(
							yc.getInputStream()));
			
			in.close();
			
			}
			catch(Exception e){
				System.out.println(s);
			}
		}
	}
}
